package spring_test;

import java.lang.reflect.Method;

import org.aopalliance.intercept.MethodInterceptor;
import org.aopalliance.intercept.MethodInvocation;
import org.springframework.aop.MethodBeforeAdvice;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class AOPTest {
	public static void main(String[] args)		{
		ApplicationContext context = new ClassPathXmlApplicationContext("spring_test/aop_proxy_config.xml");
		
		//ProxyFactoryBean test
//		SimpleBean simpleBean = (SimpleBean) context.getBean("simpleBean");
//		simpleBean.test();
		
		//auto proxy test
		DependencyBean dependencyBean = (DependencyBean) context.getBean("dependencyTarget02");
		dependencyBean.sayHi();
		System.out.println("************************");
		dependencyBean.greet();
	}
}

class DependencyBean		{
	public void sayHi()	{
		System.out.println("hello..");
	}

	public void greet()			{
		System.out.println("have a nice day..");
	}
}

class SimpleBean		{
	private DependencyBean dependencyBean;
	
	public void test()			{
		dependencyBean.sayHi();
		dependencyBean.greet();
	}

	public void setDependencyBean(DependencyBean dependencyBean) {
		this.dependencyBean = dependencyBean;
	}
}


class SimpleAdvice implements MethodBeforeAdvice		{

	@Override
	public void before(Method method, Object[] arg1, Object arg2)
			throws Throwable {
		// TODO Auto-generated method stub
		System.out.println("(interceptor01)current processing : " + method);
	}
	
}

class MyAdvice implements MethodInterceptor		{

	@Override
	public Object invoke(MethodInvocation arg0) throws Throwable {
		// TODO Auto-generated method stub
		System.out.println("(interceptor02)AOP before method invocation");
		try		{
			arg0.proceed();
			System.out.println("(interceptor02)AOP after method invocation");
		}catch(Exception e)		{
			System.out.println("(interceptor02)AOP when method throws exception");
		}
		return null;
	}
	
}


